Basic Operators in Python

When it comes to programming in Python, one of the most important concepts to understand is the use of operators. Operators are used to perform various operations on variables, values, and other elements in a program. In this article, we will discuss the basic operators in Python, their syntax, and examples of their usage.

Arithmetic Operators

Arithmetic operators are used to perform basic mathematical operations such as addition, subtraction, multiplication, and division. The basic arithmetic operators in Python are as follows:

  1. Addition (+)
  2. Subtraction (-)
  3. Multiplication (*)
  4. Division (/)
  5. Modulus (%)
  6. Exponentiation (**)
  7. Floor Division (//)

Example:

a = 10 b = 5 # Addition print(a + b) # Output: 15 # Subtraction print(a - b) # Output: 5 # Multiplication print(a * b) # Output: 50 # Division print(a / b) # Output: 2.0 # Modulus print(a % b) # Output: 0 # Exponentiation print(a ** b) # Output: 100000 # Floor Division print(a // b) # Output: 2

Comparison Operators

Comparison operators are used to compare two values or variables. The result of a comparison operator is either True or False. The basic comparison operators in Python are as follows:

  1. Equal to (==)
  2. Not equal to (!=)
  3. Greater than (>)
  4. Less than (<)
  5. Greater than or equal to (>=)
  6. Less than or equal to (<=)

Example:

a = 10 b = 5 # Equal to print(a == b) # Output: False # Not equal to print(a != b) # Output: True # Greater than print(a > b) # Output: True # Less than print(a < b) # Output: False # Greater than or equal to print(a >= b) # Output: True # Less than or equal to print(a <= b) # Output: False

Logical Operators

Logical operators are used to combine multiple conditions or values and evaluate them as a single expression. The basic logical operators in Python are as follows:

  1. And (and)
  2. Or (or)
  3. Not (not)

Example:

a = True b = False # And print(a and b) # Output: False # Or print(a or b) # Output: True # Not print(not a) # Output: False

Assignment Operators

Assignment operators are used to assign values to variables. The basic assignment operators in Python are as follows:

  1. Assignment (=)
  2. Addition assignment (+=)
  3. Subtraction assignment (-=)
  4. Multiplication assignment (*=)
  5. Division assignment (/=)
  6. Modulus assignment (%=)
  7. Exponentiation assignment (**=)
  8. Floor division assignment (//=)

Example:

a = 10 b = 5 # Assignment c = a print(c) # Output: 10 # Addition assignment a += b print(a) # Output: 15 # Subtraction assignment a -= b print(a) # Output: 10 # Multiplication assignment a *= b print(a) # Output: 50 # Division assignment a /= b print(a) # Output: 10.0 # Modulus assignment a %= b print(a) # Output: 0 # Exponentiation assignment a **= b print(a) # Output: 0 # Floor division assignment a //= b print(a) # Output: 0

Conclusion

In this article, we discussed the basic operators in Python, including arithmetic, comparison, logical, and assignment operators. Understanding these operators is essential for writing effective Python programs. By using operators in your programs, you can perform various operations, manipulate data, and control the flow of your program.

基本的な演算子[JA]